<form>
<div class="titre_h1"><h1>Messagerie</h1></div>
</form>
<?php
if(isset($_POST['envoyerMessages'])){
	$resultat = $this->sendMessages();
}
if(isset($_POST['supprimerMessages'])){
	$this->supprimerMessages();
}
if($this->request->action=='index' && empty($this->request->params)){
		$resultats = $this->getListeMessages();
		$donnee = $resultats['donnee'];
		?>
  
  <form method="post" id="messagesForm">
  <div class="legend_formulaire"><legend>Boîte de recèption</legend></div>
  <table cellpadding="0" cellspacing="0" border="0" class="display" id="messages"> 
    <thead> 
        <tr> 
            <th>Sélection</th>
            <th>Expediteur</th> 
            <th>Sujet</th> 
            <th>Date de réception</th>
        </tr> 
    </thead> 
    <tbody>
    <?php
		for($i=0;$i<count($donnee);$i++){
			$entree = $donnee[$i];
			$dateTime = formateDateTime($entree->date_ajout);
			if($entree->id_expediteur==0){
				$nom = 'Inconnu';
			} else {
				$nom = $this->getNom($entree->id_expediteur);
			}
			$url = SITE."messages/index/".$entree->id_messages;
			echo '<tr>
					<td><div class="squaredOne">
					<input type="checkbox" name="chexbox'.$entree->id_messages.'" id="'.$entree->id_messages.'id" />
					<label for="'.$entree->id_messages.'id"></label>
					</div>
					</td>
				    <td><a href="'.$url.'" class="msg" id="'.$entree->id_messages.'">'.
					$nom.'</a></td>
					<td><a href="'.$url.'" class="msg" id="'.$entree->id_messages.'">'.$entree->sujet.'</a></td>
					<td> Le '.$dateTime['jour'].'/'.$dateTime['mois'].'/'.$dateTime['annee'].' à '.$dateTime['heure'].
					':'.$dateTime['minute'].':'.$dateTime['seconde'].'</td>
			</tr>';
		}
	?>
    </tbody>
    <tfoot> 
        <tr> 
            <th>Sélection</th>
            <th>Expediteur</th> 
            <th>Sujet</th> 
            <th>Date de réception</th>
        </tr> 
    </tfoot> 
    </table><br />
    <?php
	if(count($donnee)!=0){
		?>
    <button class="btn btn-danger"  type="submit"  name="supprimerMessages" onClick="return confirm('Voulez-vous vraiment supprimer les messages selectionnés')"><span class="glyphicon glyphicon-trash">&nbsp;</span>Supprimer sélection</button>
    <?php
	}?>
    </form>
   
    <?php
} else if($this->request->action=='index' && count($this->request->params)==1){
	$id_messages = intval($this->request->params[0]);
	$messages = $this->getMessages($id_messages);
	
	if(!$messages==null || ! empty($messages)){
		$dateTime = formateDateTime($messages->date_ajout);
		
	?>
    <form method="post" action="<?php echo SITE.'messages' ?>">
    	<?php if(!empty($resultat) && isset($_POST['envoyerMessages'])){ 
		echo "<pre class='success'>Message envoyé !</pre>";
		} ?>
    	<div class="legend_formulaire"><legend>Boîte de recèption</legend></div>
    	<table cellpadding="0" cellspacing="0" border="0" class="display" id="afficherMessages"> 
    	<thead> 
        	<tr> 
            <th>Expediteur</th> 
            <th>Sujet</th> 
            <th>Date de recèption</th>
       	 </tr> 
    	</thead> 
        <tbody>
        	<?php
			$url = SITE."/messages/index/".$messages->id_messages;
			if($messages->id_expediteur==0){
				$nom = NOM_SITE;
			} else {
				$nom = $this->getNom($messages->id_expediteur);
			}
			echo '<tr>
					<td><a href="'.$url.'" class="msg" id="'.$messages->id_messages.'">'.
					$nom.'</a></td>
					<td><a href="" class="msg">'.$messages->sujet.'</a></td>
					<td> Le '.$dateTime['jour'].'/'.$dateTime['mois'].'/'.$dateTime['annee'].' à '.$dateTime['heure'].':'.$dateTime['minute'].'</td>
			</tr>';
			?>
        </tbody>
        <tfoot>
        </tfoot>
       </table> <br />
       <p>
       
        <?php echo $messages->contenu ?>
       </p><hr />
       		<input type="checkbox" name="<?php echo 'chexbox'.$id_messages; ?>" checked style="display:none" />
       		<button class="btn btn-info"  type="submit" name="retour"><span class="glyphicon glyphicon-arrow-left"></span>&nbsp;Retour aux messages</button>	
      	 	<button class="btn btn-danger"  type="submit" name="supprimerMessages"><span class="glyphicon glyphicon-trash"></span>&nbsp;Supprimer ce message</button>
    	
        </form>
               
        <form action="<?php echo $this->request->url ?>" method="post">
        <div class="legend_formulaire"><legend>Répondre au message</legend></div>
        	<table class="table table-hover">
            <tr><td>
        	<label>Sujet </label></td><td>
            <input type="text" required name="sujet" id="sujet" placeholder="sujet de votre message" class="form-control"/></td></tr><tr><td>
            <label>Messages  </label></td><td>
            <textarea name="messages" id="messagesText" required placeholder="Votre messages" class="form-control"></textarea></td></tr></table>
            
            <button class="btn btn-success"  type="submit" name="envoyerMessages"><span class="glyphicon glyphicon-send"></span>&nbsp;Envoyer</button>
            <button class="btn btn-danger"  type="reset">Éffacer</button>
        
    </form>
   <?php
	}
}
?>
